import axios from "axios";
import store from "@/store";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import { getUserTempId } from "../utils/getUserTempId";
const ajax = axios.create({
  baseURL: "/api",
  timeout: 5000,
});
ajax.interceptors.request.use((config) => {
  NProgress.start();
  config.headers.userTempId = getUserTempId();
  const token = store.state.user.userInfo.token;
  if (token) {
    config.headers.token = token;
  }
  return config;
});
ajax.interceptors.response.use(
  (response) => {
    NProgress.done();
    return response.data;
  },
  (error) => {
    NProgress.done();
    return Promise.reject(new Error(error.message));
  }
);
export default ajax;
